//
//  CTVChannelSwitchInfoController.m
//  Channltv
//
//  Created by Bruno Keymolen on 27/11/07.
//  Copyright 2007 Bruno Keymolen. All rights reserved.
//
//           [ /\ ]
// [c-] [b-] [/--\] [b+] [c+]
//        Documentaries
//
#import "CTVChannelSwitchInfoController.h"
#import "CTVNavigationManager.h"
#import "CTVCache.h"

@implementation CTVChannelSwitchInfoController
- (id) initWithScene: (BRRenderScene *)scene andChannel:(CTVChannel*)channel{
	[super initWithScene:scene];

	m_channel = channel;
	[m_channel retain];

	
	//Build GUI elements
	//Title
	m_titlecontrol = [[BRTextControl alloc] initWithScene: scene];
	[m_titlecontrol setText:[m_channel name]];
	[m_titlecontrol setTextAttributes:[[BRThemeInfo sharedTheme] mediumTextAttributes]];
	[self addControl:m_titlecontrol];

	m_subTitlecontrol = [[BRTextControl alloc] initWithScene: scene];
	[m_subTitlecontrol setText:[m_channel name]];
	[m_subTitlecontrol setTextAttributes:[[BRThemeInfo sharedTheme] smallTextAttributes]];
	[self addControl:m_subTitlecontrol];


	//channel img
	m_thisChannelImg = [[BRImageControl alloc] initWithScene: [self scene]];
	[m_thisChannelImg setReflectionAmount:0.0f];
	[m_thisChannelImg setReflectionOffset:0.0f];
	[self addControl:m_thisChannelImg];
	
	//channel next 1 img
	m_nextChannel1Img = [[BRImageControl alloc] initWithScene: [self scene]];
	[self addControl:m_nextChannel1Img];

	//channel prev 1 img
	m_prevChannel1Img = [[BRImageControl alloc] initWithScene: [self scene]];
	[self addControl:m_prevChannel1Img];

	//channel next 2 img
	m_nextChannel2Img = [[BRImageControl alloc] initWithScene: [self scene]];
	[self addControl:m_nextChannel2Img];

	//channel prev 2 img
	m_prevChannel2Img = [[BRImageControl alloc] initWithScene: [self scene]];
	[self addControl:m_prevChannel2Img];


	[self _layout];

	m_selectTime = 1.6;
 	
	//Destroy ourselves within  m_selectTime seconds
	m_timer = [NSTimer scheduledTimerWithTimeInterval:m_selectTime target:self selector:@selector(_timerHandler:) userInfo:nil repeats:NO];
	[m_timer retain];

	
	return self;
}

- (void) dealloc{

	[m_titlecontrol release];
	[m_subTitlecontrol release];
	[m_channel release];
	[m_timer invalidate];
	[m_timer release];

	[m_thisChannelImg release];
	[m_nextChannel1Img release];
	[m_prevChannel1Img release];
	[m_nextChannel2Img release];
	[m_prevChannel2Img release];	
	
	[super dealloc];
}

-(BOOL)brEventAction:(BREvent*)event{
	BOOL ret = NO;

	//We are only interested in the key-down events.
	if([event value] == 0){
		return [super brEventAction:event];
	}

	//ret = [super brEventAction:event];
	
	switch([event remoteAction]){
		case kBRRemoteActionPlayPause:
			ret = YES; //ignore
			[[self stack] removeController:self];	
			break;
		case kBRRemoteActionMenu:
			ret = YES; //ignore
			break;
		case kBRRemoteActionDown:
		case kBRRemoteActionLeft:
			ret = YES;				
//			[m_timer invalidate];		
//			m_timer = [NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:@selector(_prevChannel:) userInfo:nil repeats:NO];
[self _prevChannel:nil];		
			break;
		case kBRRemoteActionUp:
		case kBRRemoteActionRight:
			ret = YES;
//			[m_timer invalidate];		
//			m_timer = [NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:@selector(_nextChannel:) userInfo:nil repeats:NO];
[self _nextChannel:nil];		
			break;		
	}
	 
	   
	if(ret == FALSE){  
		ret = [super brEventAction:event];
	}
	
	return ret;
}



- (void) setFrame: (NSRect) frame{
    [[super masterLayer] setFrame: frame];

	[self _layout];
}




//Private
-(void)_layout{
	CTVChannel* tmpChannel;
	NSRect masterFrame = [[self masterLayer] frame];
	NSRect titleframe = masterFrame;    	
	
	[m_titlecontrol setText:[m_channel name]];
	NSSize titleTextSize = [m_titlecontrol renderedSize];
	titleframe.size.height = titleTextSize.height;
	titleframe.origin.y = (masterFrame.size.height * 0.40f) + (titleframe.size.height * 0.50f);
	titleframe.origin.x = (masterFrame.size.width - titleTextSize.width) * 0.5f;
	[m_titlecontrol setFrame:titleframe];
	
	NSRect subtitleframe = masterFrame;    	
	NSString* clipTitle = [[m_channel currentClipToPlay] getTitle];
	[m_subTitlecontrol setText:clipTitle];
	NSSize subTitleTextSize = [m_subTitlecontrol renderedSize];
	subtitleframe.size.height = subTitleTextSize.height;
	subtitleframe.origin.y = titleframe.origin.y - subTitleTextSize.height - 20;
	subtitleframe.origin.x = (masterFrame.size.width - subTitleTextSize.width) * 0.5f;
	[m_subTitlecontrol setFrame:subtitleframe];
	
	
	//img
	[m_thisChannelImg setTexture:[m_channel thumbTextureForScene:[self scene]]];
	NSRect img1Rect;
	img1Rect.size.height = masterFrame.size.height * 0.30;
	img1Rect.size.width = img1Rect.size.height;
	img1Rect.origin.x =  (masterFrame.size.width - img1Rect.size.width) * 0.5f;
	img1Rect.origin.y = titleframe.origin.y + titleframe.size.height + 20;
	[m_thisChannelImg setFrame:img1Rect];
	
	tmpChannel = [[CTVNavigationManager GetInstance] getDistanceFromChannel:nil distance:-1]; 
	[m_prevChannel1Img setTexture:[tmpChannel thumbTextureForScene:[self scene]]];
	NSRect imgLeft1Rect = img1Rect;
	imgLeft1Rect.size.height = masterFrame.size.height * 0.20;
	imgLeft1Rect.size.width = imgLeft1Rect.size.height;
	imgLeft1Rect.origin.x =  img1Rect.origin.x - imgLeft1Rect.size.width - 30;
	[m_prevChannel1Img setFrame:imgLeft1Rect];


	tmpChannel = [[CTVNavigationManager GetInstance] getDistanceFromChannel:nil distance:1]; 
	[m_nextChannel1Img setTexture:[tmpChannel thumbTextureForScene:[self scene]]];
	NSRect imgRight1Rect = imgLeft1Rect;
	imgRight1Rect.origin.x =  img1Rect.origin.x + img1Rect.size.width + 30;
	[m_nextChannel1Img setFrame:imgRight1Rect];

	tmpChannel = [[CTVNavigationManager GetInstance] getDistanceFromChannel:nil distance:-2]; 
	[m_prevChannel2Img setTexture:[tmpChannel thumbTextureForScene:[self scene]]];
	NSRect imgLeft2Rect = imgLeft1Rect;
	imgLeft2Rect.size.height = masterFrame.size.height * 0.15;
	imgLeft2Rect.size.width = imgLeft2Rect.size.height;
	imgLeft2Rect.origin.x =  imgLeft1Rect.origin.x - imgLeft2Rect.size.width - 30;
	[m_prevChannel2Img setFrame:imgLeft2Rect];

	tmpChannel = [[CTVNavigationManager GetInstance] getDistanceFromChannel:nil distance:+2]; 
	[m_nextChannel2Img setTexture:[tmpChannel thumbTextureForScene:[self scene]]];
	NSRect imgRight2Rect = imgLeft2Rect;
	imgRight2Rect.origin.x =  imgRight1Rect.origin.x + imgRight1Rect.size.width + 30;
	[m_nextChannel2Img setFrame:imgRight2Rect];
	
	
}


-(void)_timerHandler:(NSTimer*)timer{
	[[self stack] removeController:self];	
}


-(void)_nextChannel:(NSTimer*)timer{
	[m_timer invalidate];		
	
	//[m_timer release];
	m_channel = [[CTVNavigationManager GetInstance] nextChannel];
	[self _layout];
	[[self scene] renderScene]; 	
	m_timer = [NSTimer scheduledTimerWithTimeInterval:m_selectTime target:self selector:@selector(_timerHandler:) userInfo:nil repeats:NO];
	[m_timer retain];
}


-(void)_prevChannel:(NSTimer*)timer{
	[m_timer invalidate];		
	//[m_timer release];
	m_channel = [[CTVNavigationManager GetInstance] prevChannel];
	[self _layout];
	[[self scene] renderScene]; 
	m_timer = [NSTimer scheduledTimerWithTimeInterval:m_selectTime target:self selector:@selector(_timerHandler:) userInfo:nil repeats:NO];
	[m_timer retain];
}



@end
